package com.situ.crm.rbac.utils;

import com.situ.crm.bean.vo.PermissionVO;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

public class PermissionTreeUtil {

    public static List<PermissionVO> buildTree(List<PermissionVO> permissionVOList) {
        List<PermissionVO> treeList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(permissionVOList)) {
            for (PermissionVO permissionVO : permissionVOList) {
                //递归的入口pid==0
                if (permissionVO.getPid() == 0) {
                    permissionVO.setChildren(buildChildTree(permissionVO, permissionVOList));
                    treeList.add(permissionVO);
                }
            }
        }

        return treeList;
    }

    //从父节点递归查询子节点
    // 判断permissionVO.id=pid是否相等，如果相等，就是判断permissionVO的孩子，封装到判断permissionVO的children集合里面
    private static List<PermissionVO> buildChildTree(PermissionVO permissionVO, List<PermissionVO> permissionVOList) {
        List<PermissionVO> childTree = new ArrayList<>();
        for (PermissionVO perm : permissionVOList) {
            if (permissionVO.getId().longValue() == perm.getPid().longValue()) {
                perm.setChildren(buildChildTree(perm, permissionVOList));
                childTree.add(perm);
            }
        }
        return childTree;
    }

    public static void main(String[] args) {
        // Long类型比较大小:1、调用longValue 2、使用equals
        Long num1 = 21212323232L;
        Long num2 = 21212323232L;
        System.out.println(num1 == num2);//false
        System.out.println(num1.longValue() == num2.longValue());//true
        System.out.println(num1.equals(num2));//true
    }

}
